<template>
  <view class="wrap">
    <view
        class="pictrue"
        v-for="(item, index) in imgList || []"
        :key="index + 'img'"
    >
      <image :src="item" mode="aspectFill"></image>
      <!-- 删除按钮 -->
      <text class="btndel ebao-iconfont ebao-ic_close text-red" @click="handleRemove(index)"></text>
    </view>
    <!-- 添加图片 -->
    <view
        class="upLoad acea-row row-center-wrapper"
        @click="modalPicTap('duo')"
    >
      <text>+</text>
    </view>
    <view class="footer">
      <view class="btn-default btn-blue" @click="addStoreProductImg">
        保存
      </view>
    </view>
  </view>
</template>

<script>
import {addStoreProductImg} from "@/api/admin";
import {getProductDetail} from "@/api/store";

export default {
  data() {
    return {
      imgList:[],
      id:'',
    }
  },
  onLoad(options) {
    this.id=options.id
    this.getProductDetail()
  },
  onShow() {

  },
  methods: {
    // 上传图片
    uploadpic: function() {
      let that = this;
      that.$util.uploadImageChange({count:10,url:'upload/image'}, (res) => {
        this.imgList.push(res.data.url);
      });
    },
    // 打开选择图片弹窗
    modalPicTap(tit, type) {
      this.uploadpic()
    },
    // 删除图片
    handleRemove(i, type) {
      this.imgList.splice(i, 1);
    },
    addStoreProductImg(){
      addStoreProductImg({
        product_id:this.id,
        snapshot:this.imgList.join(',')
      }).then(res=>{
        if(res.status==200){
          uni.showToast({
            title:'上传成功',
            icon:'none'
          })
          uni.navigateBack()
        }
      })
    },
    // 请求详情
    getProductDetail(){
      getProductDetail(this.id, {}).then(res=>{
        if(res.status===200){
          let goods=res.data.storeInfo
          if(goods.snapshot){
            let a= JSON.parse(goods.snapshot)
            this.imgList = a.split(',')
          }
        }
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.wrap{
  padding: 30rpx;
  display: flex;
  flex-wrap: wrap;
}
.pictrue {
  width: 200rpx;
  height: 200rpx;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 15px;
  margin-bottom: 10px;
  display: inline-block;
  position: relative;
  cursor: pointer;

  image {
    width: 100%;
    height: 100%;
  }

  .btndel {
    position: absolute;
    z-index: 1;
    right: 0;
    top: -4px;
  }
}
.upLoad {
  width: 200rpx;
  height: 200rpx;
  line-height: 58px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.02);
  cursor: pointer;
  text-align: center;
  font-size: 24px;
  color: #999;
}
.footer{
  position: fixed;
  bottom: env(safe-area-inset-bottom);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  .btn-default{
    width: 200rpx;
    height: 66rpx;
    font-size: 28rpx;
  }
}
</style>